#include "stdafx.h"
#include "CppUnitTest.h"
#include "PublicInterfaces\{FAFD3CB8-FCF3-46EE-B32E-45A4FF68073E}\IImageComputePluginLib.h"
#include "MxPluginManager\MxPluginManager1.h"
#include "PublicInterfaces\{FAFD3CB8-FCF3-46EE-B32E-45A4FF68073E}\IImageCompute.h"

#include "..\..\PublicInterfaces\PluginICPopenCV\IPluginICPopenCV.h"
#include "..\..\PublicInterfaces\PluginICPopenCV\OCV-Dev-ProductID.h"

using namespace Microsoft::VisualStudio::CppUnitTestFramework;

namespace PluginICPopenCVTest
{
	TEST_CLASS(IPluginICPopenCVTest)
	{
	private:
		MxPluginManager			*_pluginMngr;
		MxPluginLib				*_plugin;
		IPluginICPopenCV		*_iPluginICPopenCV;
		IImageComputePluginLib	*_lib;
		IImageCompute		*_icp;

	public:

		TEST_METHOD_INITIALIZE(IPluginICPopenCVInit)
		{
			_pluginMngr = new MxPluginManager();
			if (_pluginMngr->RefreshPluginsInRootFolder( SID_ImageProc, true) == true)
			{
				if ((_plugin = _pluginMngr->GetPlugin(PRODID_PluginICPopenCV)) != nullptr)
				{
					if ((_iPluginICPopenCV = static_cast<IPluginICPopenCV *>(_plugin->CreateClassInstance(IPluginICPopenCVIID::Guid()))) != nullptr)
					{
						if((_lib = static_cast<IImageComputePluginLib *>(_plugin->CreateClassInstance(IImageComputePluginLibIID::Guid()))) != nullptr)
						{
							_icp = _lib->GetIImageCompute();
						}
					}
				}
			}
		}

		TEST_METHOD_CLEANUP(IPluginICPopenCVDeinit)
		{
			if(_icp != nullptr)
				_icp->DestroyPtr();
			_icp = nullptr;

			if(_iPluginICPopenCV != nullptr)
				_iPluginICPopenCV->DestroyPtr();
			_iPluginICPopenCV = nullptr;

			if(_lib != nullptr)
				_lib->DestroyPtr();
			_lib = nullptr;

			if (_pluginMngr != nullptr)
				delete _pluginMngr;
			_pluginMngr = nullptr;
		}
		
		TEST_METHOD(IPluginICPopenCVCreateTest)
		{
			Assert::IsNotNull(_pluginMngr);
			Assert::IsNotNull(_plugin);
			Assert::IsTrue(_plugin->IsValid());

			Assert::IsNotNull(_iPluginICPopenCV);

			Assert::IsNotNull(_lib);
			Assert::IsTrue(_lib->IsValid());

			Assert::IsNotNull(_icp);
			Assert::IsTrue(_icp->IsValid());

		}

		TEST_METHOD(IPluginICPopenCVDetailsTest)
		{
			Assert::IsNotNull(_iPluginICPopenCV);

			Assert::AreEqual("PluginICPopenCV", _iPluginICPopenCV->GetLibName());
			Assert::AreEqual("1.3.34.0", _iPluginICPopenCV->GetLibVersion());
			Assert::AreEqual("Will Stott Copyright 2015", _iPluginICPopenCV->GetLibCopyrightNotice());
			Assert::AreEqual("Maximodex Limited", _iPluginICPopenCV->GetLibOwnerName());
			Assert::AreEqual("New BSD Licence", _iPluginICPopenCV->GetLibLicenseName());
			Assert::AreEqual("http://PluginICPopenCV.codeplex.com/license", _iPluginICPopenCV->GetLibLicenseURL());
			Assert::AreEqual("http://www.PluginICPopenCV.org", _iPluginICPopenCV->GetLibSupportSite());

			Assert::AreEqual("{2ADD8170-FB21-47B5-B282-29602950C257}",_iPluginICPopenCV->GetLibID());
			Assert::AreEqual(4,_iPluginICPopenCV->GetInterfaceCount());
			Assert::IsTrue(MxGUID::IsEqual(IPluginICPopenCVGUID,_iPluginICPopenCV->GetIID(0)));
			Assert::IsTrue(MxGUID::IsEqual(IImageComputePluginLibGUID,_iPluginICPopenCV->GetIID(1)));
			Assert::IsTrue(MxGUID::IsEqual(IMxPluginLibDetailsGUID,_iPluginICPopenCV->GetIID(2)));
			Assert::IsTrue(MxGUID::IsEqual(IMxPluginBaseGUID,_iPluginICPopenCV->GetIID(3)));
		}

		TEST_METHOD(IImageComputePluginLibTest)
		{
			Assert::IsNotNull(_lib);

			Assert::AreEqual("{2ADD8170-FB21-47B5-B282-29602950C257}",_lib->GetProductID());
			Assert::AreEqual("1.3.34.0", _lib->GetVersion());
			Assert::AreEqual("PluginICPopenCV", _lib->GetProductName());
			Assert::AreEqual("ImageCompute Proc for OpenCV library",_lib->GetDescription());
			Assert::AreEqual("OpenCV", _lib->GetToolboxLabel());
			Assert::AreEqual(100, _lib->GetToolboxIconID());
		}

		TEST_METHOD(IImageComputeTest)
		{
			Assert::IsNotNull(_icp);

			Assert::AreEqual(25, _icp->GetFunctionCount());

			GUID FnIDBrighten = MxGUID::ConvertFromString("{E7104626-038F-4BB6-A7E3-9DB823673436}");
			Assert::IsTrue(MxGUID::IsEqual( FnIDBrighten,_icp->GetFunctionID(0)));
			Assert::IsTrue(_icp->IsExistIImageComputeFn(FnIDBrighten));

			GUID FnIDContrast = MxGUID::ConvertFromString("{30F9C62C-EDBF-4B00-8973-648490C13218}");
			Assert::IsTrue(MxGUID::IsEqual( FnIDContrast,_icp->GetFunctionID(1)));
			Assert::IsTrue(_icp->IsExistIImageComputeFn(FnIDContrast));

			GUID FnIDGaussian = MxGUID::ConvertFromString("{4CCF13FA-B96F-4B79-9C7B-464A40722BEC}");
			Assert::IsTrue(MxGUID::IsEqual( FnIDGaussian,_icp->GetFunctionID(2)));
			Assert::IsTrue(_icp->IsExistIImageComputeFn(FnIDGaussian));

			GUID FnIDStretch = MxGUID::ConvertFromString("{E9C2D74B-162B-45D6-AF42-807611AF214B}");
			Assert::IsTrue(MxGUID::IsEqual( FnIDStretch,_icp->GetFunctionID(3)));
			Assert::IsTrue(_icp->IsExistIImageComputeFn(FnIDStretch));

			GUID FnIDMakeEllipsePts = MxGUID::ConvertFromString("{6A022824-68BC-44B1-A0D5-D2411827DC80}");
			Assert::IsTrue(MxGUID::IsEqual(FnIDMakeEllipsePts, _icp->GetFunctionID(4)));
			Assert::IsTrue(_icp->IsExistIImageComputeFn(FnIDMakeEllipsePts));

			GUID FnIDSnake = MxGUID::ConvertFromString("{66D56DA2-0DD0-4617-9036-61FBC78BF0A0}");
			Assert::IsTrue(MxGUID::IsEqual( FnIDSnake,_icp->GetFunctionID(5)));
			Assert::IsTrue(_icp->IsExistIImageComputeFn(FnIDSnake));

			GUID FnBilateralFilter = MxGUID::ConvertFromString("{C245540B-3B20-4A4E-8AE4-5DF17A09BA14}");
			Assert::IsTrue(MxGUID::IsEqual(FnBilateralFilter, _icp->GetFunctionID(6)));
			Assert::IsTrue(_icp->IsExistIImageComputeFn(FnBilateralFilter));

			GUID FnIDErode = MxGUID::ConvertFromString("{B4EE518D-BFE4-44FF-B4B5-88A776A29861}");
			Assert::IsTrue(MxGUID::IsEqual(FnIDErode, _icp->GetFunctionID(7)));
			Assert::IsTrue(_icp->IsExistIImageComputeFn(FnIDErode));

			GUID FnIDDilate = MxGUID::ConvertFromString("{D78F924E-FD0E-4233-B1DA-F04A1E8261D1}");
			Assert::IsTrue(MxGUID::IsEqual(FnIDDilate, _icp->GetFunctionID(8)));
			Assert::IsTrue(_icp->IsExistIImageComputeFn(FnIDDilate));

			GUID FnIDGrayscale = MxGUID::ConvertFromString("{5562F011-E582-4AE2-9B60-F236E74D2054}");
			Assert::IsTrue(MxGUID::IsEqual(FnIDGrayscale, _icp->GetFunctionID(9)));
			Assert::IsTrue(_icp->IsExistIImageComputeFn(FnIDGrayscale));

			GUID FnIDYCrCb = MxGUID::ConvertFromString("{E2C440F4-A4F7-48C4-A0A2-6B48A6C460E8}");
			Assert::IsTrue(MxGUID::IsEqual(FnIDYCrCb, _icp->GetFunctionID(10)));
			Assert::IsTrue(_icp->IsExistIImageComputeFn(FnIDYCrCb));

			GUID FnIDThreshold = MxGUID::ConvertFromString("{730BCB32-4021-45FE-9420-CBFBF91CA328}");
			Assert::IsTrue(MxGUID::IsEqual(FnIDThreshold, _icp->GetFunctionID(11)));
			Assert::IsTrue(_icp->IsExistIImageComputeFn(FnIDThreshold));

			GUID FnIDClosing = MxGUID::ConvertFromString("{D1B42775-7D77-425B-884C-834A4BB415F9}");
			Assert::IsTrue(MxGUID::IsEqual(FnIDClosing, _icp->GetFunctionID(12)));
			Assert::IsTrue(_icp->IsExistIImageComputeFn(FnIDClosing));

			GUID FnIDOpening = MxGUID::ConvertFromString("{32A5D5C4-B12C-49FD-A2E2-51A2F8523C31}");
			Assert::IsTrue(MxGUID::IsEqual(FnIDOpening, _icp->GetFunctionID(13)));
			Assert::IsTrue(_icp->IsExistIImageComputeFn(FnIDOpening));

			GUID FnIDMorphologicalGradient = MxGUID::ConvertFromString("{E9581591-A000-4810-817F-DF79D5985038}");
			Assert::IsTrue(MxGUID::IsEqual(FnIDMorphologicalGradient, _icp->GetFunctionID(14)));
			Assert::IsTrue(_icp->IsExistIImageComputeFn(FnIDMorphologicalGradient));

			GUID FnIDDenoise = MxGUID::ConvertFromString("{99EED209-7981-43C7-A606-1F82DE4BCD09}");
			Assert::IsTrue(MxGUID::IsEqual(FnIDDenoise, _icp->GetFunctionID(15)));
			Assert::IsTrue(_icp->IsExistIImageComputeFn(FnIDDenoise));

			GUID FnIDEqualizeHistogram = MxGUID::ConvertFromString("{AD685E85-5C74-4153-9133-7CA698D95570}");
			Assert::IsTrue(MxGUID::IsEqual(FnIDEqualizeHistogram, _icp->GetFunctionID(16)));
			Assert::IsTrue(_icp->IsExistIImageComputeFn(FnIDEqualizeHistogram));

			GUID FnIDFloodfill = MxGUID::ConvertFromString("{18F4AC4F-B042-45F1-A67E-0644BF301A48}");
			Assert::IsTrue(MxGUID::IsEqual(FnIDFloodfill, _icp->GetFunctionID(17)));
			Assert::IsTrue(_icp->IsExistIImageComputeFn(FnIDFloodfill));

			GUID FnIDLaplacianSharp = MxGUID::ConvertFromString("{B29B2979-44B4-4095-BA2F-557BEFAD59AB}");
			Assert::IsTrue(MxGUID::IsEqual(FnIDLaplacianSharp, _icp->GetFunctionID(18)));
			Assert::IsTrue(_icp->IsExistIImageComputeFn(FnIDLaplacianSharp));

			GUID FnIDSobelSharp = MxGUID::ConvertFromString("{7F2C1B87-9903-49B0-863C-ED5D325F40E5}");
			Assert::IsTrue(MxGUID::IsEqual(FnIDSobelSharp, _icp->GetFunctionID(19)));
			Assert::IsTrue(_icp->IsExistIImageComputeFn(FnIDSobelSharp));

			GUID FnIDPyramidUp = MxGUID::ConvertFromString("{9EDA536B-A6E6-4101-9EAD-D239A9123F45}");
			Assert::IsTrue(MxGUID::IsEqual(FnIDPyramidUp, _icp->GetFunctionID(20)));
			Assert::IsTrue(_icp->IsExistIImageComputeFn(FnIDPyramidUp));

			GUID FnIDPyramidDown = MxGUID::ConvertFromString("{448F8BFD-CA35-445C-87A0-F9C90D83165B}");
			Assert::IsTrue(MxGUID::IsEqual(FnIDPyramidDown, _icp->GetFunctionID(21)));
			Assert::IsTrue(_icp->IsExistIImageComputeFn(FnIDPyramidDown));

			GUID FnIDRemap = MxGUID::ConvertFromString("{CBE5BD11-C293-4ADF-A482-5D02D01AC818}");
			Assert::IsTrue(MxGUID::IsEqual(FnIDRemap, _icp->GetFunctionID(22)));
			Assert::IsTrue(_icp->IsExistIImageComputeFn(FnIDRemap));

			GUID FnIDRotate = MxGUID::ConvertFromString("{660C03EF-5F82-4630-A3BE-F78A329F4653}");
			Assert::IsTrue(MxGUID::IsEqual(FnIDRotate, _icp->GetFunctionID(23)));
			Assert::IsTrue(_icp->IsExistIImageComputeFn(FnIDRotate));

			GUID FnIDTransform = MxGUID::ConvertFromString("{B6627EBE-A40E-48F2-BB3B-EE49856C035D}");
			Assert::IsTrue(MxGUID::IsEqual(FnIDTransform, _icp->GetFunctionID(24)));
			Assert::IsTrue(_icp->IsExistIImageComputeFn(FnIDTransform));
		}

		TEST_METHOD(IImageComputeSaveIPSTest)
		{
			Assert::IsNotNull(_icp);
			Assert::IsTrue(_icp->IsValid());
			Assert::IsTrue(_icp->SaveImagePluginSpec("..\\..\\..\\TestImages\\PluginICPOpenCV.ips"));
		}

	};
}